/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By zhanglin
* 注意：
* 本软件为zhanglin开发研制
 */
package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"shop/model"
	"shop/util"
	"time"
)

//增加
func AddMenu(c *gin.Context) {
	var vo model.Menu
	if err := c.BindWith(&vo, binding.JSON); err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("参数解析错误"))
		return
	}
	local := time.Now().Local()
	vo.CreateTime = local
	//vo.UpdateTime = local
	id, err := vo.InsertMenu()
	if err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("保存失败"))
		return
	}
	m := make(map[string]interface{})
	m["id"] = id
	c.JSON(http.StatusOK, new(util.Response).Ok("保存成功", m))
}

// 删除
func DeleteMenu(c *gin.Context) {
	var vo model.Menu
	id, _ := util.StringToInt64(c.Param("id"))
	if err := vo.DeleteMenu(id); err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("删除失败"))
		return
	}
	c.JSON(http.StatusOK, new(util.Response).Ok("修改成功", nil))
}

// 更新
func UpdateMenu(c *gin.Context) {
	var vo model.Menu
	if err := c.BindWith(&vo, binding.JSON); err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("参数解析错误"))
		return
	}
	if err := vo.UpdateMenu(); err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("更新失败"))
		return
	}
	c.JSON(http.StatusOK, new(util.Response).Ok("", nil))
}

//分页
func SplitPageMenuList(c *gin.Context) {
	// 获取参数
	pageSize, _ := util.StringToInt64(c.DefaultQuery("pageSize", "10"))
	pageIndex, _ := util.StringToInt64(c.DefaultQuery("pageIndex", "1"))
	var vo model.Menu
	m, err := vo.SplitPageMenuList(pageIndex, pageSize)
	if err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("查询失败"))
		return
	}
	c.JSON(http.StatusOK, new(util.Response).Ok("查询成功", m))
}
